import psutil  # 进程运行情况
import os
from socket import gethostname
import time


# import platform  # 包含系统信息查询函数
# import logging


class ServiceInformation:
    # def __init__():
        # self.cpu_dist = {}
        # self.mem_dist = {}
        # self.disk_dist = {}
        # self.obtain_cpu(self)
        # self.obtain_memory(self)
        # self.obain_disk(self)
        # get hostname
    hostname = gethostname()

    # obtain cpu utilization
    def obtain_cpu():
        cpu_utilization = psutil.cpu_percent()
        cpu_dist = {"cpu_utilization": cpu_utilization}
        # print(cpu_utilization)
        # print(cpu_dist)
        return cpu_dist

    # obtain memory utilization
    def obtain_memory():
        svmem = psutil.virtual_memory()
        mem_u = svmem.used
        mem_f = svmem.free
        mem_t = svmem.total
        mem_dist = {"mem_utilization": mem_u / mem_t * 100, "mem_free": mem_f, "mem_total": mem_t, "mem_u": mem_u}
        # print(mem_dist)
        return mem_dist

    # obtain disk used
    def obain_disk():
        stinfo = os.statvfs('/')
        disk_total = stinfo.f_bsize * stinfo.f_blocks / 1024 / 1024 / 1024
        disk_free = stinfo.f_bavail * stinfo.f_frsize / 1024 / 1024 / 1024
        disk_used = disk_total - disk_free
        disk_dist = {"disk_total": disk_total, "disk_free": disk_free, "disk_used": disk_used, "disk_utilization": disk_used / disk_total}
        # print(disk_dist)
        return disk_dist



if __name__ == '__main__':
    time.sleep(2)
    ServiceInformation.obtain_cpu()
    ServiceInformation.obtain_memory()
    ServiceInformation.obain_disk()
